﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using PayPal;
using PayPal.Api;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Able.Api.Tests.Zhifu
{
    [TestClass]
    public class QueryPayPal
    {
        string clientId = "Af5HjgbZdzd2MUSXEmhtetNYvRi0yJLBam91l-uZLrBL3Rr7S1K8mQTcxYSyLWZ5dM38S_qe3UlGLL8g";
        string secret = "EFLDLJUzSDnzWziMp26wXGWZyf4jQ1sgZ732s2q7DpRDkjIhHboG8fjVhlh1TmIcY-cccV3BDgcf8CxH";
        [TestMethod]
        public void QueryPayment()
        {
            var apiContext = GetApi(clientId, secret);
            string paymentid = "PAY-3RF07930WJ229050VK5PCT3I";
            var payment = Payment.Get(apiContext, paymentid);
            Console.WriteLine(JsonConvert.SerializeObject(payment, new JsonSerializerSettings { Formatting = Formatting.Indented }));
            var transaction = payment.transactions.FirstOrDefault();
            if (transaction == null)
            {

            }
            var orderno = transaction;
            var currency = transaction.amount.currency;
            var sumprice = payment.transactions.Sum(s =>
            {
                decimal totalPrice;
                decimal.TryParse(s.amount.total, out totalPrice);
                return totalPrice;
            });

        }

        #region PayPal
        private string GetAccessToken(string clientId, string secret)
        {

            string token = new OAuthTokenCredential
                            (
                               clientId,
                                secret,
                                GetConfiguration()
                            ).GetAccessToken();
            return token;
        }
        private APIContext GetApi(string clientId, string secret)
        {
            APIContext context = new APIContext(GetAccessToken(clientId, secret));
            context.Config = GetConfiguration();
            return context;
        }
        public Dictionary<string, string> GetConfiguration()
        {
            Dictionary<string, string> configurationMap = new Dictionary<string, string>();
            configurationMap.Add("mode", "sandbox");
            return configurationMap;
        }
        #endregion
    }
}
